package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketSearchHistory;
import com.cskaoyan.market.db.domain.MarketSearchHistoryExample;
import com.cskaoyan.market.db.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.market.service.AdminSearchHistoryService;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminSearchHistoryServiceImpl implements AdminSearchHistoryService {


    @Override
    public List<MarketSearchHistory> list(String id, String username, String keyword, String from, String addTime, String updateTime,String page, String limit) {

        SqlSession session = MybatisUtils.getSession();
        MarketSearchHistoryMapper marketSearchHistoryMapper = session.getMapper(MarketSearchHistoryMapper.class);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();

        //根据用户ID和关键字模糊查询
        if(!id.isEmpty()&&keyword.isEmpty()) {
            criteria.andUserIdEqualTo(Integer.parseInt(id));
        }
        if(id.isEmpty()&&!keyword.isEmpty()) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if(!id.isEmpty()&&!keyword.isEmpty()) {
            criteria.andUserIdEqualTo(Integer.parseInt(id));
            criteria.andKeywordLike("%" + keyword + "%");
        }

        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        session.close();
        return marketSearchHistories;


    }
}
